<?php

/**
 * This is the model class for table "blogcategory".
 *
 * The followings are the available columns in table 'blogcategory':
 * @property integer $id
 * @property string $title
 * @property string $url
 * @property integer $sort_1
 * @property integer $paginate
 * @property string $meta_title
 * @property string $meta_description
 * @property string $meta_keywords
 * @property integer $status
 * @property integer $create_time
 * @property integer $public_time
 * @property string $access
 */
class Blogcategory extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Blogcategory the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{blogcategory}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, url', 'required'),
            array('title, url', 'unique'),
			array('sort_1, paginate, status, create_time', 'numerical', 'integerOnly'=>true),
			array('title, url, access, meta_title, meta_keywords', 'length', 'max'=>255),
            array('public_time, meta_description', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, url, status, create_time, public_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Название категории',
            'url'=>'URL',
			'sort_1' => 'Сортировка',
			'paginate' => 'Постраничка',
            'meta_title'=>'Meta title',
            'meta_description'=>'Meta description',
            'meta_keywords'=>'Meta keywords',
			'status' => 'Статус',
			'create_time' => 'Дата и время создания',
			'public_time' => 'Дата публикации',
			'access' => 'Доступ',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
        $criteria->compare('url',$this->url,true);
//		$criteria->compare('sort_1',$this->sort_1);
//		$criteria->compare('paginate',$this->paginate);
//        $criteria->compare('meta_title',$this->meta_title,true);
//        $criteria->compare('meta_description',$this->meta_description,true);
//        $criteria->compare('meta_keywords',$this->meta_keywords,true);
		$criteria->compare('status',$this->status);
//		$criteria->compare('create_time',$this->create_time);
//		$criteria->compare('public_time',$this->public_time);
//		$criteria->compare('access',$this->access,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    
     public static function all()   //Функция возвращает список категорий (Применяется для связанных моделей)
    {
        $models = self::model()->findAll();
        $array = array();
            foreach($models as $item)
            {
                $array[$item->id] = $item->title;
            }
        return $array;   
    }
    
    
    
    
    public function beforeSave()
    {
        if($this->isNewRecord)
            {
                $this->create_time = time();
            }
         $this->public_time = strtotime($this->public_time);    //Преобразует текстовое время в INT и записывает целое число в БД
        
        return parent::beforeSave();
    }
    
    
     protected function afterFind()                             //Функция преобразует формат даты и возвращает в $data->public_time
    {
     $date = date('d-m-Y', $this->public_time);
     $this->public_time = $date;
     parent::afterFind();
    }
    
    
    
    
}